package com.sh.shanhuo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.sh.shanhuo.R;
import com.sh.shanhuo.bean.BaseBean;
import com.sh.shanhuo.listener.GsonObjectCallback;
import com.sh.shanhuo.utils.OkHttpUtil;
import com.sh.shanhuo.utils.Tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class FeedbackActivity extends BaseActivity {
    private String appUserName;
    private String appUserId;
    private String appNickName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        super.initView();
        getBundle();
        ImageView back = findViewById(R.id.back);
        ImageView commit = findViewById(R.id.commit);
        EditText content = findViewById(R.id.content);
        EditText phone = findViewById(R.id.phone);
        back.setOnClickListener(v -> finish());
        commit.setOnClickListener(v -> commit(
                content.getText().toString().trim(),
                phone.getText().toString().trim()));
    }

    private void getBundle() {
        Intent intent = getIntent();
        if (intent == null)
            return;
        Bundle bundle = intent.getExtras();
        if (bundle == null)
            return;
        appUserName = bundle.getString("appUserName", "");
        appUserId = bundle.getString("appUserId", "");
        appNickName = bundle.getString("appNickName", "");
    }

    private void commit(String content, String contact) {
        onShowLoading();
        String uuid = Tools.getUniquePsuedoID();
        Map<String, String> params = new HashMap<>();
        params.put("appUserName", appUserName);
        params.put("appUserId", appUserId);
        params.put("appNickName", appNickName);
        params.put("contact", contact);
        params.put("content", content);
        params.put("phoneUuid", uuid);
        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.USER_FEEDBACK, params, new GsonObjectCallback<BaseBean>() {
            @Override
            public void onUi(BaseBean baseBean) {
                onDismissLoading();
                finish();
            }

            @Override
            public void onFailed(Call call, IOException e) {
                onDismissLoading();
                Toast.makeText(FeedbackActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}